package cn.sixos.bo;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.forten.utils.common.StringUtil;
import org.forten.utils.system.BeanPropertyUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.sixos.dao.HibernateDao;
import cn.sixos.entity.Course;
import cn.sixos.util.PageInfo;
import cn.sixos.vo.CourseVo;
import cn.sixos.vo.CourseVoForCount;

@Service("adminBo")
public class AdminBo {
	@Resource
	private HibernateDao hibernateDao;

	// 保存
	// 参数：Course实体
	// 返回：无
	@Transactional
	public void doSave(Course course) {
		// Dao执行保存
		hibernateDao.save(course);
		// 无返回
	}
	
	//删除课程
	//参数：long id
	//返回：无
	@Transactional
	public void doDelete(long id){
		hibernateDao.delete(Course.class, id);
	}
	
	//课程Update
	//参数：CourseVo
	//返回：无
	@Transactional
	public void doUpdate(CourseVo vo){
		//通过CourseVo的id查询出Course实体
		Course course = hibernateDao.findById(Course.class, vo.getId());
		//将vo copy到实体上
		BeanPropertyUtil.copy(course,vo);
		//执行update
		hibernateDao.update(course);
	}
	
	//查询课程ForUpdate
	//参数：long id
	//返回：CourseVo
	@Transactional(readOnly=true)
	public CourseVo queryCourse(long id){
		//根据id查询出Course实体
		Course course = hibernateDao.findById(Course.class, id);
		//将查询出的Course copy到vo
		CourseVo vo = new CourseVo();
		BeanPropertyUtil.copy(vo,course);
		//返回
		return vo;
	}
	
	
	// 条件查询，分页
	// 参数：courseName,teacher,minTime,maxTime,pageNo,pageSize
	// 返回：名为result的Map
	@Transactional(readOnly = true)
	public Map<String, Object> queryAll(String courseName, String teacher,
			Date minTime, Date maxTime, Integer pageNo, Integer pageSize) {
		// 定义返回结果容器
		Map<String, Object> result = new HashMap<String, Object>();
		// 定义数据结果容器
		List<CourseVo> voList = new ArrayList<>();
		// 组装查询逻辑
		String hql = "FROM Course WHERE WHERE 1=1 ";
		String countHql = "SELECT count(id) FROM Course WHERE 1=1 ";
		Map<String, Object> params = new HashMap<String, Object>();
		if (StringUtil.hasText(courseName)) {
			hql = hql + "AND courseName LIKE :c ";
			countHql = countHql + "AND courseName LIKE :c ";
			params.put("c", "%" + courseName + "%");
		}
		if (StringUtil.hasText(teacher)) {
			hql = hql + "AND teacher LIKE :t ";
			countHql = countHql + "AND teacher LIKE :t ";
			params.put("t", "%" + teacher + "%");
		}
		if (minTime != null && maxTime != null) {
			hql = hql + "AND startTime BETWEEN :minTime AND :maxTime ";
			countHql = countHql
					+ "AND startTime BETWEEN :minTime AND :maxTime ";
			params.put("minTime", minTime);
			params.put("maxTime", maxTime);
		}

		// 取符合条件的数据数量
		long count = hibernateDao.findUniqueObject(countHql, params);
		// 若符合条件的数据数量是0则返回空的结果容器
		if (count == 0) {
			return result;
		}

		// 计算分页数据
		PageInfo page = PageInfo.getInstance(pageNo, pageSize, count);

		// 查询数据
		List<Course> list = hibernateDao.findBy(hql, params,
				(int) page.getFirstResultNum(), pageSize);

		// 转换VO
		for (Course course : list) {
			CourseVo vo = new CourseVo();
			BeanPropertyUtil.copy(vo, course);
			voList.add(vo);
		}

		// 封装结果数据
		result.put("voList", voList);
		result.put("page", page);

		// 返回
		return result;
	}

	
	//课程统计
	//参数：无
	//返回：voList
	@Transactional(readOnly=true)
	public List<CourseVoForCount> doCount(){
		List<CourseVoForCount> voList =  new ArrayList<>();
		//组装查询语句
		String hql = "FROM Course c WHERE c.userSet IS NOT EMPTY";
		// 查询所有已经被学生选过的课程（查询userSet不为空的那些课程对象）
		List<Course> list = hibernateDao.find(hql);
		// 遍历所有已经被学生选过的课程
		for (Course course : list) {
			CourseVoForCount vo = new CourseVoForCount();
			// 获得选择课程的学生的个数
			int countUser =course.getUserSet().size();
			//获得课程容量
			int courseVolume=course.getCourseVolume();
			//将各个属性set到vo
			vo.setId(course.getId());
			vo.setCourseName(course.getCourseName());
			vo.setCountUser(countUser);
			vo.setCourseVolume(courseVolume);
			vo.setTeacher(course.getTeacher());
			//根据选课所占比例set vo的状态（前台需要显示调用）
			if(countUser>(courseVolume*0.9)){
				vo.setState(1);
			}
			else if(countUser<(courseVolume*0.5)){
				vo.setState(2);
			}else{
				vo.setState(3);
			}
			//将vo添加到voList
			voList.add(vo);
		}
		
		return voList;
	}
	
}
